package com.infomanage.service.tech;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.TechRewardDAO;
import com.infomanage.dao.TechSecurityDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.TechSecurity;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.FileUpload;
import com.infomanage.util.StringUtil;
import com.infomanage.util.smartupload.SmartRequest;
import com.infomanage.util.smartupload.SmartUpload;
import com.infomanage.vo.TechSecurityItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class TechSecurityService {

	public static TechSecurityItem loadTechSecurity(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			TechSecurityDAO dao = new TechSecurityDAO();
			if (StringUtils.isEmpty(itemId))
				return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<TechSecurityItem> loadTechSecurityList(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			TechSecurityDAO dao = new TechSecurityDAO();
			List<TechSecurityItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveTechSecurity(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			TechSecurity bean = new TechSecurity();
			bean.setActionDetail(request.getParameter("actionDetail"));
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setExaminePerson(request.getParameter("examinePerson"));
			bean.setImplementPerson(request.getParameter("implementPerson"));
			bean.setParticipants(request.getParameter("participants"));
			bean.setPlace(request.getParameter("place"));
			bean.setPlanName(request.getParameter("planName"));
			bean.setSchematic(request.getParameter("schematic"));
			bean.setTime(request.getParameter("time"));
			bean.setTimestamp(request.getParameter("timestamp"));
			bean.setTreatDept(request.getParameter("treatDept"));
			bean.setWorkSubject(request.getParameter("workSubject"));
			bean.setXdCommanderId(Integer.parseInt(request.getParameter("xdCommanderId")));
			bean.setZdCommanderId(Integer.parseInt(request.getParameter("zdCommanderId")));
			TechSecurityDAO dao = new TechSecurityDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeTechSecurity(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			TechSecurityDAO dao = new TechSecurityDAO();
			dao.remove(TechSecurity.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeBatchTechSecurity(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			TechSecurityDAO dao = new TechSecurityDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(TechSecurity.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject modifyTechSecurity(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			TechSecurityDAO dao = new TechSecurityDAO();
			TechSecurity bean = dao.find(TechSecurity.class, itemId);
			bean.setActionDetail(request.getParameter("actionDetail"));
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setExaminePerson(request.getParameter("examinePerson"));
			bean.setImplementPerson(request.getParameter("implementPerson"));
			bean.setParticipants(request.getParameter("participants"));
			bean.setPlace(request.getParameter("place"));
			bean.setPlanName(request.getParameter("planName"));
			bean.setSchematic(request.getParameter("schematic"));
			bean.setTime(request.getParameter("time"));
			bean.setTimestamp(request.getParameter("timestamp"));
			bean.setTreatDept(request.getParameter("treatDept"));
			bean.setWorkSubject(request.getParameter("workSubject"));
			bean.setXdCommanderId(Integer.parseInt(request.getParameter("xdCommanderId")));
			bean.setZdCommanderId(Integer.parseInt(request.getParameter("zdCommanderId")));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static List<TechSecurityItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			TechSecurityDAO dao = new TechSecurityDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<TechSecurityItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			TechSecurityDAO dao = new TechSecurityDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String timestamp = request.getParameter("timestamp");
		String deptId = request.getParameter("deptId");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(timestamp)) {
			FieldBean field = new FieldBean("timestamp", timestamp);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
	
	public static ReturnObject uploadLegend(HttpServletRequest httpRequest, HttpServletResponse response, PageContext pageContext){
		ReturnObject retObj = new ReturnObject();
		try {
			httpRequest.setCharacterEncoding("UTF-8");
			String actionType = httpRequest.getParameter("actionType");
			if (actionType == null) {
				FileUpload fileUpload = new FileUpload("jpg,gif,png,bmp,dwg");
				SmartUpload smartUpload = fileUpload.upload(httpRequest,
						pageContext);
				SmartRequest request = smartUpload.getRequest();
				actionType = request.getParameter("actionType");
				String[] fileNames = smartUpload.getFileNames();
				if (fileNames != null && fileNames.length > 0) {
					String dir = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
					retObj.SetErrored(false);
					retObj.setErrorMessage(dir);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			retObj.setErrorMessage(e.getMessage());
		}
		return retObj;
	}
}
